#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy  as np

eps = 1e-8

def signal(*args):
    # ZhenFuBear
    df = args[0]
    n = args[1]
    factor_name = args[2]

    high = df[['close', 'open']].max(axis=1)
    low = df[['close', 'open']].min(axis=1)
    high = high.rolling(n, min_periods=1).max()
    high = high.shift(1)
    low = low.rolling(n, min_periods=1).min()
    low = low.shift(1)
    df[factor_name] = (low - df['close']) / (df['close'] + eps)

    return df
